package com.ruoyi.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.*;
import com.ruoyi.mes.utils.CodeUtils;
import com.ruoyi.mes.vo.plan.MtrPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.service.IMtrPlanService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 物料计划Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Service
public class MtrPlanServiceImpl implements IMtrPlanService {
    @Autowired
    private ProInfoMapper proInfoMapper;

    @Autowired
    private ProOrderMapper proOrderMapper;

    @Autowired
    private ProgPlanDetailMapper progPlanDetailMapper;

    @Autowired
    private ProgPlanMapper progPlanMapper;

    @Autowired
    private BuyNeedMapper buyNeedMapper;

    @Autowired
    private MtrStockMapper mtrStockMapper;

    @Autowired
    private MtrPlanMapper mtrPlanMapper;

    /**
     * 查询物料计划
     *
     * @param id 物料计划主键
     * @return 物料计划
     */
    @Override
    public MtrPlan selectMtrPlanById(Long id) {
        return mtrPlanMapper.selectMtrPlanById(id);
    }

    /**
     * 查询物料计划列表
     *
     * @param mtrPlan 物料计划
     * @return 物料计划
     */
    @Override
    public List<MtrPlan> selectMtrPlanList(MtrPlan mtrPlan) {
        return mtrPlanMapper.selectMtrPlanList(mtrPlan);
    }

    /**
     * 新增物料计划
     *
     * @param mtrPlan 物料计划
     * @return 结果
     */
    @Override
    public int insertMtrPlan(MtrPlan mtrPlan) {
        mtrPlan.setCreateTime(DateUtils.getNowDate());
        return mtrPlanMapper.insertMtrPlan(mtrPlan);
    }

    /**
     * 修改物料计划
     *
     * @param mtrPlan 物料计划
     * @return 结果
     */
    @Override
    public int updateMtrPlan(MtrPlan mtrPlan) {
        mtrPlan.setUpdateTime(DateUtils.getNowDate());
        return mtrPlanMapper.updateMtrPlan(mtrPlan);
    }

    /**
     * 批量删除物料计划
     *
     * @param ids 需要删除的物料计划主键
     * @return 结果
     */
    @Override
    public int deleteMtrPlanByIds(Long[] ids) {
        return mtrPlanMapper.deleteMtrPlanByIds(ids);
    }

    /**
     * 删除物料计划信息
     *
     * @param id 物料计划主键
     * @return 结果
     */
    @Override
    public int deleteMtrPlanById(Long id) {
        return mtrPlanMapper.deleteMtrPlanById(id);
    }


    /**
     * 生成采购需求
     *
     * @param id 物料计划id
     * @return
     */
    @Override
    @Transactional
    public AjaxResult createBuyNeed(Long id) {
        MtrPlanVo mtrPlanVo = getDetails(id);
        List<MtrInfo> mtrs = mtrPlanVo.getMtrs();

        //生成采购需求
        String buyNeedCode = CodeUtils.BuyNeedCodeCreat();
        for (MtrInfo mtr : mtrs) {
            BuyNeed buyNeed = new BuyNeed();
            buyNeed.setNeedCode(buyNeedCode);
            buyNeed.setProgId(mtrPlanVo.getProgId());
            buyNeed.setMtrCode(mtr.getMtrCode());
            buyNeed.setMtrName(mtr.getMtrName());
            buyNeed.setMtrPlanCode(mtrPlanVo.getCode());

            buyNeed.setNeedQuantity(mtr.getMtrQuantity());
            buyNeed.setStatus(0L);//默认未采购
            buyNeedMapper.insertBuyNeed(buyNeed);
        }
        //生成后修改物料计划状态 - 已下达
        MtrPlan mtrPlan = mtrPlanMapper.selectMtrPlanById(id);
        List<MtrPlan> mtrPlans = mtrPlanMapper.getMtrPlansByCode(mtrPlan.getCode());
        for (MtrPlan plan : mtrPlans) {
            plan.setStatus(1L);
            mtrPlanMapper.updateMtrPlan(plan);
        }

        return AjaxResult.success("采购需求生成成功!");
    }

    /**
     * 查 - 物料计划编码相同的数据
     *
     * @param code 物料计划编码
     * @return
     */
    @Override
    public List<MtrPlan> getMtrPlansByCode(String code) {
        return mtrPlanMapper.getMtrPlansByCode(code);
    }

    /**
     * 查 - 物料计划分页 - 物料计划编号
     *
     * @param mtrPlan
     * @return
     */
    @Override
    public List<MtrPlan> mtrPlanList(MtrPlan mtrPlan) {
        List<MtrPlan> mtrPlans = mtrPlanMapper.mtrPlanList(mtrPlan);
        if (mtrPlans.size() != 0) {
            for (MtrPlan plan : mtrPlans) {
                //查找对应的生产计划
                ProgPlanDetail detail = progPlanDetailMapper.selectProgPlanDetailById(plan.getProgDetailId());
                ProgPlan progPlan = progPlanMapper.selectProgPlanById(detail.getProgId());
                //查找对应的产品订单 - 产品订单编号及产品id
                ProOrder proOrder = proOrderMapper.selectProOrdersByCodeAndProId(progPlan.getOrderNumber(), progPlan.getProId());
                plan.setProOrder(proOrder);
                //查找对应的产品信息
                ProInfo proInfo = proInfoMapper.selectProInfoById(progPlan.getProId());
                plan.setProInfo(proInfo);
            }
        }
        return mtrPlans;
    }


    /**
     * 查 - 物料计划详情 - 物料计划id
     *
     * @param id
     * @return
     */
    @Override
    public MtrPlanVo getDetails(Long id) {
        MtrPlanVo mtrPlanVo = new MtrPlanVo();

        MtrPlan mtrPlan = mtrPlanMapper.selectMtrPlanById(id);
        mtrPlanVo.setCode(mtrPlan.getCode());
        ProgPlanDetail detail = progPlanDetailMapper.selectProgPlanDetailById(mtrPlan.getProgDetailId());
        mtrPlanVo.setProgId(detail.getProgId());
        List<MtrPlan> mtrPlans = mtrPlanMapper.getMtrPlansByCode(mtrPlan.getCode());

        List<MtrInfo> mtrs = new ArrayList<>();
        for (MtrPlan plan : mtrPlans) {
            MtrInfo mtrInfo = new MtrInfo();
            BeanUtils.copyProperties(plan, mtrInfo);
            mtrInfo.setMtrCode(plan.getMtrCode());
            mtrs.add(mtrInfo);
        }
        mtrPlanVo.setMtrs(mtrs);
        return mtrPlanVo;
    }

    /**
     * 撤回 - 物料计划及采购需求
     *
     * @param id 物料计划id
     * @return
     */
    @Override
    @Transactional
    public AjaxResult revokePlanAndNeed(Long id) {
        MtrPlan plan = mtrPlanMapper.selectMtrPlanById(id);
        MtrPlan mtrPlan = new MtrPlan();
        mtrPlan.setCode(plan.getCode());
        //修改物料计划状态
        List<MtrPlan> mtrPlans = mtrPlanMapper.selectMtrPlanList(mtrPlan);
        for (MtrPlan mtrPlan1 : mtrPlans) {
            mtrPlan1.setStatus(0L);
            mtrPlanMapper.updateMtrPlan(mtrPlan1);
        }

        BuyNeed buyNeed = new BuyNeed();
        buyNeed.setMtrPlanCode(plan.getCode());
        //删除采购需求订单
        List<BuyNeed> buyNeeds = buyNeedMapper.selectBuyNeedList(buyNeed);
        for (BuyNeed need : buyNeeds) {
            buyNeedMapper.deleteBuyNeedByNeedId(need.getNeedId());
        }
//        //撤回后更新生产计划明细状态 - 已关闭
//        ProgPlanDetail detail = progPlanDetailMapper.selectProgPlanDetailById(plan.getProgDetailId());
//        detail.setStatus(0L);
//        progPlanDetailMapper.updateProgPlanDetail(detail);

        return AjaxResult.success("撤回成功！");
    }

    /**
     * 查 - 物料计划 - 生产计划明细id
     *
     * @param id
     * @return
     */
    @Override
    public List<MtrPlan> getMtrPlanByProgId(Long id) {
        return mtrPlanMapper.getMtrPlanByProgId(id);
    }
}
